package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataComment;
import cn.lanqiao.bigDate.mapper.data.DataCommMapper;
import cn.lanqiao.bigDate.service.data.DataCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

@Service
public class DataCommentServiceImpl implements DataCommentService {
    @Resource
    private DataCommMapper dataCommMapper;
    @Override
    public IPage<DataComment> findByPage(Page<DataComment> page, String searchDeptName) {
        QueryWrapper<DataComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        queryWrapper.like("name",searchDeptName);
        Page<DataComment> dataCommentPage = dataCommMapper.selectPage(page,queryWrapper);
        return dataCommentPage;
    }

    @Override
    public DataComment findDataCommOne(Long id) {
        QueryWrapper<DataComment> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        DataComment dataComment = dataCommMapper.selectOne(wrapper);
        return dataComment;
    }

    @Override
    public Boolean updateDataComment(DataComment dataComment) {
        if (dataComment != null && dataComment.getId()!=null) {
            Long id = dataComment.getId();
            DataComment dataCommentOne = findDataCommOne(id);
            dataCommentOne.setName(dataComment.getName());
            dataCommentOne.setDate(dataComment.getDate());
            dataCommentOne.setComment(dataComment.getComment());
            dataCommentOne.setSubComment(dataComment.getSubComment());
            dataCommentOne.setHeart(dataComment.getHeart());
            dataCommentOne.setCreateTime(dataComment.getCreateTime());
            dataCommentOne.setCreateBy(dataComment.getCreateBy());
//            dataCommentOne.setCreateTime(LocalDateTime.now());
            dataCommentOne.setUpdateTime(LocalDateTime.now());
            dataCommentOne.setUpdateBy(dataComment.getUpdateBy());
            int i = dataCommMapper.updateById(dataComment);
            if(i==1){
                return true;
            }
            return false;
        }
        return false;
    }

    @SneakyThrows
    @Override
    public Boolean saveDataComment(DataComment dataComment) {
        dataComment.setId(dataComment.getId());
        dataComment.setName(dataComment.getName());
        String dateStr = dataComment.getDateStr();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(dateStr.replace("T", " "),df);//文本转时间
        dataComment.setDate(ldt);
        dataComment.setComment(dataComment.getComment());
        dataComment.setSubComment(dataComment.getSubComment());
        dataComment.setHeart(dataComment.getHeart());
        dataComment.setCreateTime(LocalDateTime.now());
        dataComment.setCreateBy(dataComment.getCreateBy());
        int i = dataCommMapper.insert(dataComment);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean deleteDataComment(String ids) {
        if(!StringUtils.isNullOrEmpty(ids)){
            String[] id = ids.split(",");
            System.out.println(Arrays.toString(id));
            List<String> strings = Arrays.asList(id); //将数组转换成列表
            int i = dataCommMapper.deleteBatchIds(strings);
            if(i>0){
                return true;
            }
        }
        return false;
    }
}
